package com.ruoyi.field.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.reservation.domain.Reservation;
import com.ruoyi.reservation.mapper.ReservationMapper;
import com.ruoyi.venue.domain.Venue;
import com.ruoyi.venue.mapper.VenueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.field.mapper.FieldMapper;
import com.ruoyi.field.domain.Field;
import com.ruoyi.field.service.IFieldService;
import org.springframework.util.CollectionUtils;

/**
 * 场地Service业务层处理
 * 
 * @author HuLongHui
 * @date 2023-02-18
 */
@Service
public class FieldServiceImpl implements IFieldService 
{
    @Autowired
    private FieldMapper fieldMapper;
    @Autowired
    private VenueMapper venueMapper;

    @Autowired
    private ReservationMapper reservationMapper;
    /**
     * 查询场地
     * 
     * @param fieldId 场地主键
     * @return 场地
     */
    @Override
    public Field selectFieldByFieldId(Long fieldId)
    {
        return fieldMapper.selectFieldByFieldId(fieldId);
    }

    /**
     * 查询场地列表
     * 
     * @param field 场地
     * @return 场地
     */
    @Override
    public List<Field> selectFieldList(Field field)
    {
        return fieldMapper.selectFieldList(field);
    }

    /**
     * 新增场地
     * 
     * @param field 场地
     * @return 结果
     */
    @Override
    public int insertField(Field field)
    {
        field.setCreateTime(DateUtils.getNowDate());
        return fieldMapper.insertField(field);
    }

    /**
     * 修改场地
     * 
     * @param field 场地
     * @return 结果
     */
    @Override
    public int updateField(Field field)
    {
        field.setUpdateTime(DateUtils.getNowDate());
        return fieldMapper.updateField(field);
    }

    /**
     * 批量删除场地
     * 
     * @param fieldIds 需要删除的场地主键
     * @return 结果
     */
    @Override
    public int deleteFieldByFieldIds(Long[] fieldIds)
    {
        return fieldMapper.deleteFieldByFieldIds(fieldIds);
    }

    /**
     * 删除场地信息
     * 
     * @param fieldId 场地主键
     * @return 结果
     */
    @Override
    public int deleteFieldByFieldId(Long fieldId)
    {
        return fieldMapper.deleteFieldByFieldId(fieldId);
    }

    /**
     * @param field
     * @return
     */
    @Override
    public int addVenueReservation(Field field) {

        Reservation r=new Reservation();
        r.setFieldId(field.getFieldId());
        r.setReservationTime(field.getTime());
        List<Reservation> reservations = reservationMapper.selectReservationList(r);
        if (!CollectionUtils.isEmpty(reservations)){
            throw new ServiceException("该场馆已被占用,请选择其他时间预约!");
        }
        Field field1 = fieldMapper.selectFieldByFieldId(field.getFieldId());
        Venue venue = venueMapper.selectVenueByVenueId(field1.getVenueId());

        Reservation reservation=new Reservation();
        reservation.setFieldId(field.getFieldId());
        reservation.setVenueId(field1.getVenueId());
        reservation.setFieldName(field1.getFieldName());
        reservation.setVenueName(venue.getVenueName());
        reservation.setReservationTime(field.getTime());
        reservation.setCreateBy(SecurityUtils.getUsername());
        reservation.setCreateTime(new Date());

        return reservationMapper.insertReservation(reservation);
    }
}
